package com.xms.form.service;

import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.model.DependentDescriptor;
import com.xms.dependency.service.IDependencyCreaterService;
import com.xms.dependency.service.IDependencyDeleterService;
import com.xms.dependency.service.IDependencyUpdaterService;
import com.xms.form.core.FormMetaDescriptor;
import com.xms.form.core.SystemFormDefaults;
import com.xms.form.entity.SystemForm;
import com.xms.queryview.core.QueryViewDefaults;
import com.xms.ribbonbutton.core.RibbonButtonDefaults;
import com.xms.schema.core.AttributeDefaults;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * SystemFormDependency
 * </p>
 *
 * @author migo
 * @since 2021/10/25
 */
@Service
public class SystemFormDependency implements ISystemFormDependency {
    private IDependencyCreaterService _dependencyCreaterService;
    private IDependencyUpdaterService _dependencyUpdaterService;
    private IDependencyDeleterService _dependencyDeleterService;
    private IFormBuilderService _formService;
    private ISystemFormFinderService _systemFormFinderService;

    @Autowired
    public SystemFormDependency(IFormBuilderService formService,
                                ISystemFormFinderService systemFormFinderService,
                                IDependencyCreaterService dependencyCreaterService,
                                IDependencyUpdaterService dependencyUpdaterService,
                                IDependencyDeleterService dependencyDeleterService) {
        _dependencyCreaterService = dependencyCreaterService;
        _dependencyUpdaterService = dependencyUpdaterService;
        _dependencyDeleterService = dependencyDeleterService;
        _formService = formService;
        _systemFormFinderService = systemFormFinderService;
    }

    @Override
    public boolean create(SystemForm entity) throws XmsException, SQLException {
        FormMetaDescriptor formMetaDescriptor = _formService.build(entity);
        if (CollectionUtil.notEmpty(formMetaDescriptor.attributes)) {
            List<UUID> attrIds = formMetaDescriptor.attributes.stream().map(x -> x.getAttributeid()).collect(Collectors.toList());
            if (CollectionUtil.notEmpty(attrIds)) {
                //依赖于字段
                _dependencyCreaterService.create(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), AttributeDefaults.MODULE_NAME, attrIds);
            }
        }
        if (entity.getIscustombutton() && StringUtil.notEmpty(entity.getCustombuttons())) {
            List<UUID> buttonIds = JsonUtil.toList(entity.getCustombuttons(), UUID.class);
            if (CollectionUtil.notEmpty(buttonIds)) {
                //依赖于按钮
                _dependencyCreaterService.create(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), RibbonButtonDefaults.MODULE_NAME, buttonIds);
            }
        }
        //依赖于视图(单据体)
        List<UUID> queryViewIds = FormExtensions.getQueryViewIds(entity);
        if (CollectionUtil.notEmpty(queryViewIds)) {
            _dependencyCreaterService.create(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), QueryViewDefaults.MODULE_NAME, queryViewIds);
        }

        return true;
    }

    @Override
    public boolean delete(UUID... id) throws XmsDependencyException, SQLException {
        return _dependencyDeleterService.deleteByDependentId(SystemFormDefaults.MODULE_NAME, id);
    }

    @Override
    public boolean update(SystemForm entity) throws XmsException, SQLException {
        FormMetaDescriptor formMetaDescriptor = _formService.build(entity);
        if (CollectionUtil.notEmpty(formMetaDescriptor.attributes)) {
            List<UUID> attrIds = formMetaDescriptor.attributes.stream().map(x -> x.getAttributeid()).collect(Collectors.toList());
            if (CollectionUtil.notEmpty(attrIds)) {
                //依赖于字段
                _dependencyUpdaterService.update(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), AttributeDefaults.MODULE_NAME, attrIds);
            }
        }
        if (entity.getIscustombutton() && StringUtil.notEmpty(entity.getCustombuttons())) {
            List<UUID> buttonIds = JsonUtil.toList(entity.getCustombuttons(), UUID.class);
            if (CollectionUtil.notEmpty(buttonIds)) {
                //依赖于按钮
                _dependencyUpdaterService.update(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), RibbonButtonDefaults.MODULE_NAME, buttonIds);
            }
        }
        //依赖于视图(单据体)
        List<UUID> queryViewIds = FormExtensions.getQueryViewIds(entity);
        if (CollectionUtil.notEmpty(queryViewIds)) {
            _dependencyUpdaterService.update(SystemFormDefaults.MODULE_NAME, entity.getSystemformid(), QueryViewDefaults.MODULE_NAME, queryViewIds);
        }

        return true;
    }

    @Override
    public DependentDescriptor getDependent(UUID dependentId) {
        SystemForm result = _systemFormFinderService.getById(dependentId);
        return result != null ? new DependentDescriptor() {
            {
                name = result.getName();
            }
        } : null;
    }

    @Override
    public int getComponentType() {
        return ModuleCollection.getIdentity(SystemFormDefaults.MODULE_NAME);
    }
}
